package com.wuliu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuliu.auto.JSONAuthentication;
import com.wuliu.entity.ExCar;
import com.wuliu.entity.ExLx;
import com.wuliu.mapper.ExCarMapper;
import com.wuliu.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuliu
 * @since 2022-03-05
 */
@RestController
@RequestMapping("/ex-car")
public class ExCarController extends JSONAuthentication {
    @Autowired
    ExCarMapper exCarMapper;
    @ApiOperation("添加货车")
    @PostMapping("/add")
    public void addlx(HttpServletRequest request, HttpServletResponse response, @RequestParam("dressName") String dressName, @RequestParam("dressPhone") String dressPhone, @RequestParam("carNumber") String carNumber) throws Exception{
        ExCar exCar=new ExCar();
        exCar.setDressName(dressName);
        exCar.setCarNumber(carNumber);
        exCar.setDressPhone(dressPhone);
        if(exCarMapper.insert(exCar)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除货车")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId ) throws Exception{
        int i=exCarMapper.deleteById(userId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改司机姓名")
    @PostMapping("/updName")
    public void updName(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("dressName") String  dressName)throws Exception{
        ExCar exCar=exCarMapper.selectById(id);
        exCar.setDressName(dressName);
        if(exCarMapper.updateById(exCar)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改司机电话")
    @PostMapping("/updPhone")
    public void updPhone(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("phone") String  phone)throws Exception{
        ExCar exCar=exCarMapper.selectById(id);
        exCar.setDressPhone(phone);
        if(exCarMapper.updateById(exCar)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改货车状态")
    @PostMapping("/updStatus")
    public void updStatus(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("status") String  status)throws Exception{
        ExCar exCar=exCarMapper.selectById(id);
        exCar.setStatus(status);
        if(exCarMapper.updateById(exCar)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有货车")
    @PostMapping("/findAllCar")
    public void findAllCar(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<ExCar> shUserPage=new Page<>(current,size);
        Page<ExCar> shUserPages=exCarMapper.selectPage(shUserPage,null);
        List<ExCar> records = shUserPages.getRecords();
        int total=exCarMapper.selectCount(null);
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据司机姓名查找货车")
    @PostMapping("/findByName")
    public void findByName(HttpServletRequest request, HttpServletResponse response,@RequestParam("dressName") String   dressName,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("dress_name",dressName);
        Page<ExCar> shUserPage=new Page<>(current,size);
        Page<ExCar> shUserPages=exCarMapper.selectPage(shUserPage,wrapper);
        List<ExCar> records = shUserPages.getRecords();
        int total=exCarMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("该用户还没有联系人");
            this.WriteJSON(request, response, error);
        }
    }
}

